<template>
    <div class="login">
        <el-form ref="loginFormRef" :model="loginForm" :rules="loginRules" class="login-form">
            <h3 class="title">欢迎登录<span class="title-text">海南征信消息中心管理台</span></h3>
            <el-form-item prop="userAccount">
                <el-input
                    v-model.trim="loginForm.userAccount"
                    type="text"
                    auto-complete="off"
                    placeholder="账号/手机号码"
                    @keyup.enter.native="handleLogin"
                >
                </el-input>
            </el-form-item>
            <!-- oncontextmenu="return false"
                    oncopy="return false"
                    oncut="return false" -->
            <el-form-item prop="password">
                <el-input
                    v-model.trim="loginForm.password"
                    type="password"
                    auto-complete="off"
                    placeholder="密码"
                    show-password
                    @keyup.enter.native="handleLogin"
                >
                </el-input>
            </el-form-item>
            <el-form-item prop="imgCode">
                <el-input
                    v-model.trim="loginForm.imgCode"
                    auto-complete="off"
                    clearable
                    placeholder="验证码"
                    @keyup.enter.native="handleLogin"
                >
                    <template #append
                        ><img :src="codeUrl" @click="getCode" class="login-code-img"
                    /></template>
                </el-input>
                <!-- <div class="login-code">
                    <img :src="codeUrl" @click="getCode" class="login-code-img" />
                </div> -->
            </el-form-item>
            <el-form-item style="width: 100%">
                <el-button
                    :loading="loading"
                    type="primary"
                    style="width: 100%"
                    @click.native.prevent="handleLogin"
                >
                    <span v-if="!loading">登 录</span>
                    <span v-else>登 录 中...</span>
                </el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script setup lang="ts">
import * as LoginApi from '@/api/login/index';
import { Encrypt2 } from '@/utils/AESUtil';
defineOptions({ name: 'LoginForm' });
const { push } = useRouter(); // 路由
const loading = ref(false);
const codeUrl = ref('');
const loginForm = ref({
    imgCode: '', // 图形验证码
    password: '', // 用户密码
    randomCode: '', // 获取图形验证码返回的随机码
    tenantId: '3', // 1系统用户，2金融机构用户
    userAccount: '', // 用户账号
});

const loginRules = ref({
    userAccount: [{ required: true, trigger: 'blur', message: '请输入您的账号' }],
    password: [{ required: true, trigger: 'blur', message: '请输入您的密码' }],
    imgCode: [{ required: true, trigger: 'change', message: '请输入验证码' }],
});

const getCode = () => {
    LoginApi.getImageCaptcha().then((res) => {
        codeUrl.value = res.image;
        loginForm.value.randomCode = res.randomCode;
    });
};

const loginFormRef = ref();
const handleLogin = () => {
    loginFormRef.value.validate(async (valid, fields) => {
        console.log(fields);
        if (valid) {
            loading.value = true;
            const params = Object.assign({}, unref(loginForm));
            params.password = Encrypt2(params.password);
            try {
                const res = await LoginApi.pwdLogin(params);
                localStorage.setItem('TOKEN', res.token);
                localStorage.setItem('userAccount', res.userInfo.userAccount);
                push({ path: '/' });
            } catch (error: any) {
                getCode();
                ElMessage.error(error.msg);
                loading.value = false;
            }
        } else {
            console.log('ss');
        }
    });
};

onMounted(() => {
    getCode();
});
</script>

<style scoped lang="less">
.login {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100%;
}
.title {
    font-size: 30px;
    font-family: PingFang SC;
    font-weight: bold;
    color: #303133;
    margin: 0px auto 30px auto;
    .title-text {
        margin-left: 20px;
        font-size: 16px;
        font-family: PingFang SC;
        font-weight: bold;
        color: #909399;
    }
}

.login-form {
    border-radius: 6px;
    background: #ffffff;
    width: 340px;
    padding: 25px 25px 5px 25px;
}
.login-code {
    width: 90px;
    height: 30px;
    margin-left: 10px;
    float: right;
    img {
        width: 100%;
        height: 100%;
        cursor: pointer;
        vertical-align: middle;
    }
}

.login-code-img {
    height: 30px;
}
</style>
